#include "setattetimewidget.h"
#include "ui_setattetimewidget.h"

#include "attendancetimemanager.h"

#include "common.h"

#include <QDebug>
#include <QTime>
#include <QDateTime>

#include <QCoreApplication>

SetAtteTimeWidget::SetAtteTimeWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SetAtteTimeWidget)
{
    ui->setupUi(this);

    QString appPath = QCoreApplication::applicationDirPath();
    QString configPath = appPath + "/" + CONFIG_INI_FILE;
    m_manager = new AttendanceTimeManager(configPath, this);

    loadSettings();
}

SetAtteTimeWidget::~SetAtteTimeWidget()
{
    saveSettings();

    delete ui;
}


// 保存配置
bool SetAtteTimeWidget::saveSettings()
{
    // 获取信息
    // 周一
    DailySettings mondaySettings;
    mondaySettings.startTime = ui->dateTime_Monday_start->time();
    mondaySettings.endTime = ui->dateTime_Monday_end->time();
    mondaySettings.isEnabled = ui->rbtn_Monday->isChecked();
    m_manager->setDailySettings("Monday", mondaySettings);

    // 周二
    DailySettings tuesdaySettings;
    tuesdaySettings.startTime = ui->dateTime_Tuesday_start->time();
    tuesdaySettings.endTime = ui->dateTime_Tuesday_end->time();
    tuesdaySettings.isEnabled = ui->rbtn_Tuesday->isChecked();
    m_manager->setDailySettings("Tuesday", tuesdaySettings);

    // 周三
    DailySettings wednesdaySettings;
    wednesdaySettings.startTime = ui->dateTime_Wednesday_start->time();
    wednesdaySettings.endTime = ui->dateTime_Wednesday_end->time();
    wednesdaySettings.isEnabled = ui->rbtn_Wednesday->isChecked();
    m_manager->setDailySettings("Wednesday", wednesdaySettings);

    // 周四
    DailySettings thursdaySettings;
    thursdaySettings.startTime = ui->dateTime_Thursday_start->time();
    thursdaySettings.endTime = ui->dateTime_Thursday_end->time();
    thursdaySettings.isEnabled = ui->rbtn_Thursday->isChecked();
    m_manager->setDailySettings("Thursday", thursdaySettings);

    // 周五
    DailySettings fridaySettings;
    fridaySettings.startTime = ui->dateTime_Friday_start->time();
    fridaySettings.endTime = ui->dateTime_Friday_end->time();
    fridaySettings.isEnabled = ui->rbtn_Friday->isChecked();
    m_manager->setDailySettings("Friday", fridaySettings);


    // 周六
    DailySettings saturdaySettings;
    saturdaySettings.startTime = ui->dateTime_Saturday_start->time();
    saturdaySettings.endTime = ui->dateTime_Saturday_end->time();
    saturdaySettings.isEnabled = ui->rbtn_Saturday->isChecked();
    m_manager->setDailySettings("Saturday", saturdaySettings);

    // 周日
    DailySettings sundaySettings;
    sundaySettings.startTime = ui->dateTime_Sunday_start->time();
    sundaySettings.endTime = ui->dateTime_Sunday_end->time();
    sundaySettings.isEnabled = ui->rbtn_Sunday->isChecked();
    m_manager->setDailySettings("Sunday", sundaySettings);


    // 保存修改
    return m_manager->saveSettings();
}
// 加载配置
bool SetAtteTimeWidget::loadSettings()
{
    if(m_manager->loadSettings())
    {
        qDebug() << "配置加载成功！";
    } else {
        qDebug() << "配置加载失败！";
        return false;
    }

    // 读取配置
    QMap<QString, DailySettings> settings = m_manager->getAllSettings();

    // qDebug() << "config:" << settings;

    // for(DailySettings &setting : settings) {
    //     qDebug() /*<< settings.key(setting) << " "*/
    //              << setting.isEnabled << " "
    //              << setting.startTime << " "
    //              << setting.endTime << " ";
    // }

    // 展示信息
    // 周一
    DailySettings MondayInfo = settings.value("Monday");
    ui->rbtn_Monday->setChecked(MondayInfo.isEnabled);
    ui->dateTime_Monday_start->setTime(MondayInfo.startTime);
    ui->dateTime_Monday_end->setTime(MondayInfo.endTime);

    // 周二
    DailySettings TuesdayInfo = settings.value("Tuesday");
    ui->rbtn_Tuesday->setChecked(TuesdayInfo.isEnabled);
    ui->dateTime_Tuesday_start->setTime(TuesdayInfo.startTime);
    ui->dateTime_Tuesday_end->setTime(TuesdayInfo.endTime);

    // 周三
    DailySettings WednesdayInfo = settings.value("Wednesday");
    ui->rbtn_Wednesday->setChecked(WednesdayInfo.isEnabled);
    ui->dateTime_Wednesday_start->setTime(WednesdayInfo.startTime);
    ui->dateTime_Wednesday_end->setTime(WednesdayInfo.endTime);

    // 周四
    DailySettings ThursdayInfo = settings.value("Thursday");
    ui->rbtn_Thursday->setChecked(ThursdayInfo.isEnabled);
    ui->dateTime_Thursday_start->setTime(ThursdayInfo.startTime);
    ui->dateTime_Thursday_end->setTime(ThursdayInfo.endTime);

    // 周五
    DailySettings FridayInfo = settings.value("Friday");
    ui->rbtn_Friday->setChecked(FridayInfo.isEnabled);
    ui->dateTime_Friday_start->setTime(FridayInfo.startTime);
    ui->dateTime_Friday_end->setTime(FridayInfo.endTime);

    // 周六
    DailySettings SaturdayInfo = settings.value("Saturday");
    ui->rbtn_Saturday->setChecked(SaturdayInfo.isEnabled);
    ui->dateTime_Saturday_start->setTime(SaturdayInfo.startTime);
    ui->dateTime_Saturday_end->setTime(SaturdayInfo.endTime);

    // 周日
    DailySettings SundayInfo = settings.value("Sunday");
    ui->rbtn_Sunday->setChecked(SundayInfo.isEnabled);
    ui->dateTime_Sunday_start->setTime(SundayInfo.startTime);
    ui->dateTime_Sunday_end->setTime(SundayInfo.endTime);

    return true;
}

void SetAtteTimeWidget::on_btn_saveSet_clicked()
{
    if(!saveSettings())
    {
        qDebug() << "文件保存失败!";
    } else {
        qDebug() << "文件保存成功!";
    }
}

